<!--#layout name=mail-->
<div class="page-header">
    <h1 class="title">My addresses</h1>
</div>
<div class="navbar navbar-default">
    <div class="container-fluid">
        <a href="javascript:;" class="btn green navbar-btn" data-bind="click: onShowModal.bind(this, 'normal')"><i class="fa fa-plus"></i> <span>Normal</span></a>
        <a href="javascript:;" class="btn green navbar-btn" data-bind="click: onShowModal.bind(this, 'wildcard')"><i class="fa fa-plus"></i> <span>Wildcard</span></a>
        <a href="javascript:;" class="btn green navbar-btn" data-bind="click: onShowModal.bind(this, 'group')"><i class="fa fa-plus"></i> <span>Group</span></a>
        <a href="javascript:;" class="btn green navbar-btn" data-bind="click: onShowModal.bind(this, 'forward')"><i class="fa fa-plus"></i> <span>Forward</span></a>
        <a data-bind="visible: showDeleteBtn, click: onDelete" class="btn red navbar-btn">Delete</a>
    </div>
</div>
<div data-bind="component: { name: 'kb-table', params: tableData }"></div>

<div data-bind="modal: showModal" class="modal fade" data-backdrop="static" data-keyboard="false">
    <div class="modal-dialog">
        <div class="modal-content">
            <div class="modal-header">
                <button data-bind="click: resetModal" class="close"><i class="fa fa-close"></i></button>
                <h4 class="modal-title">
                    <!-- ko if: modalType() == 'normal' || modalType() == 'wildcard'-->
                    <span>New address</span>
                    <!-- /ko -->
                    <!-- ko if: modalType() == 'group' -->
                    <span>New group</span>
                    <!-- /ko -->
                    <!-- ko if: modalType() == 'forward' -->
                    <!-- ko if: forwardEditing() -->
                    <span>Edit forwarding</span>
                    <!-- /ko -->
                    <!-- ko if: !forwardEditing() -->
                    <span>New forwarding</span>
                    <!-- /ko -->
                    <!-- /ko -->
                    <!-- ko if: modalType() == 'members' -->
                    <span>Group members</span>
                    <!-- /ko -->
                </h4>
            </div>
            <div class="modal-body">
                <!-- ko if: modalType() == 'normal' -->
                <div class="form-horizontal">
                    <div class="form-group">
                        <label class="control-label col-md-2">Address</label>
                        <div class="col-md-10">
                            <div class="input-group">
                                <input type="text" data-bind="value: normalAddress, error: normalAddress, errorPlacement: 'top'" class="form-control">
                                <span class="input-group-addon">@</span>
                                <select data-bind="value: domain, error: domain, options: domains, optionsText: 'domainName', optionsValue: 'domainName'" class="form-control"></select>
                            </div>
                        </div>
                    </div>
                </div>
                <!-- /ko -->
                <!-- ko if: modalType() == 'wildcard' -->
                <div class="alert alert-info">
                    <strong>Info</strong>
                    <p>Use * to represent any characters. * is required and only one * is allowed</p>
                </div>
                <div class="form-horizontal">
                    <div class="form-group">
                        <label class="control-label col-md-3">Wildcard address</label>
                        <div class="col-md-9" data-bind="error: wildcardAddress">
                            <div class="input-group">
                                <input type="text" data-bind="value: wildcardAddress" class="form-control">
                                <span class="input-group-addon">@</span>
                                <select data-bind="value: domain, error: domain, options: domains, optionsText: 'domainName', optionsValue: 'domainName'" class="form-control"></select>
                            </div>
                        </div>
                    </div>
                </div>
                <!-- /ko -->
                <!-- ko if: modalType() == 'group' -->
                <div class="form-horizontal">
                    <div class="form-group">
                        <label class="control-label col-md-2">Group</label>
                        <div class="col-md-10" data-bind="error: groupAddress">
                            <div class="input-group">
                                <input type="text" data-bind="value: groupAddress" class="form-control">
                                <span class="input-group-addon">@</span>
                                <select data-bind="value: domain, error: domain, options: domains, optionsText: 'domainName', optionsValue: 'domainName'" class="form-control"></select>
                            </div>
                        </div>
                    </div>
                </div>
                <!-- /ko -->
                <!-- ko if: modalType() == 'forward' -->
                <div class="form-horizontal">
                    <div class="form-group">
                        <label class="control-label col-md-3">Agent address</label>
                        <div class="col-md-9">
                            <!-- ko if: !forwardEditing() -->
                            <div class="input-group">
                                <input type="text" data-bind="value: forwardName, error: forwardName" class="form-control">
                                <span class="input-group-addon">@</span>
                                <select data-bind="value: domain, error: domain, options: domains, optionsText: 'domainName', optionsValue: 'domainName'" class="form-control"></select>
                            </div>
                            <!-- /ko -->
                            <!-- ko if: forwardEditing() -->
                            <input type="text" data-bind="value: forwardName" class="form-control" disabled>
                            <!-- /ko -->
                        </div>
                    </div>
                    <div class="form-group">
                        <label class="control-label col-md-3">Forward to address</label>
                        <div class="col-md-9">
                            <input type="text" data-bind="value: forwardAddress, error: forwardAddress" class="form-control">
                        </div>
                    </div>
                </div>
                <!-- /ko -->
                <!-- ko if: modalType() == 'members' -->
                <div class="navbar navbar-default">
                    <div class="container-fluid">
                        <a href="javaccript:;" data-bind="visible: !editingMember(), click: onEditMember" class="btn green navbar-btn">Add</a>
                    </div>
                </div>
                <table class="table table-striped table-hover">
                    <thead>
                        <tr>
                            <th>Address</th>
                            <th class="table-action"></th>
                            <th class="table-action"></th>
                        </tr>
                    </thead>
                    <tbody data-bind="foreach: members">
                        <tr>
                            <!-- ko if: $data.memberAddress -->
                            <td data-bind="text: $data.address"></td>
                            <td></td>
                            <td>
                                <a href="javascript:;" data-bind="click: $parent.removeMember" class="btn btn-xs red">Remove</a>
                            </td>
                            <!-- /ko -->
                            <!-- ko ifnot: $data.memberAddress -->
                            <td>
                                <input type="text" class="form-control" data-bind="value: $data.address, error: $data.address, errorPlacement: 'left'" />
                            </td>
                            <td>
                                <a href="javascript:;" data-bind="click: $parent.saveMember" class="btn green">Save</a>
                            </td>
                            <td>
                                <a href="javascript:;" data-bind="click: $parent.cancelMember" class="btn gray">Cancel</a>
                            </td>
                            <!-- /ko -->
                        </tr>
                    </tbody>
                </table>
                <!-- /ko -->
            </div>
            <div class="modal-footer">
                <!-- ko if: modalType() == 'members' -->
                <button data-bind="click: resetModal" class="btn gray">Close</button>
                <!-- /ko -->
                <!-- ko ifnot: modalType() == 'members' -->
                <button data-bind="click: saveModal" class="btn green">Save</button>
                <button data-bind="click: resetModal" class="btn gray">Cancel</button>
                <!-- /ko -->
            </div>
        </div>
    </div>
</div>

<script>
    Kooboo.mailEditor = {};
</script>
<script>
    (function() {
        Kooboo.loadJS([
            "/_Admin/Scripts/tableModel.js",
            "/_Admin/Scripts/kooboo/Guid.js",
            "/_Admin/Scripts/components/kbTable.js"
        ])
    })()
</script>
<script src="/_Admin/View/Emails/Addresses.js"></script>